package wyh.service;

import org.apache.ibatis.session.SqlSession;
import wyh.mapper.PatientMapper;
import wyh.pojo.Patient;
import wyh.util.MyBatisUtils;

import java.util.List;

public class PatientService {
    private static SqlSession sqlSession = null;
    private static PatientMapper patientMapper = null;

    public void beforeEach() {
        sqlSession = MyBatisUtils.getSession(true);
        patientMapper = sqlSession.getMapper(PatientMapper.class);
    }

    /**
     * @param like 模糊查询模糊查询
     * @return 返回一个患者对象列表b
     */
    public List<Patient> ServicegetByNameLike(String like) {
        beforeEach();
        List<Patient> patientList = patientMapper.MappergetByNameLike(like);
        sqlSession.close();
        return patientList;
    }

    public List<Patient> ServicegetAll() {
        beforeEach();
        List<Patient> patientList = patientMapper.MappergetAll();
        sqlSession.close();
        return patientList;
    }

    public Boolean ServicedeleteById(int patientId) {
        beforeEach();
        int result = patientMapper.MapperdeleteUserById(patientId);
        sqlSession.close();
        return result > 0;
    }

    public Boolean Serviceupdate(Patient patient) {
        beforeEach();
        int result = patientMapper.MapperupdateUserById(patient);
        sqlSession.close();
        return result > 0;
    }

    public Boolean Serviceadd(Patient patient) {
        beforeEach();
        int result = patientMapper.MapperaddUser(patient);
        sqlSession.close();
        return result > 0;
    }

    public int Serviceadd2(Patient patient) {
        beforeEach();
        int r = patientMapper.MapperaddUser2(patient);
        sqlSession.close();
        if (r > 0) {
            return patient.getPatientId();
        }else {
            return -1;
        }
    }

    public Patient ServicegetById(int patientId) {
        beforeEach();
        Patient patient = patientMapper.MappergetById(patientId);
        sqlSession.close();
        return patient;
    }
}